package com.yvdedu.hospital.controller;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yvdedu.hospital.entity.Department;
import com.yvdedu.hospital.entity.Drug;
import com.yvdedu.hospital.entity.InputInfo;
import com.yvdedu.hospital.entity.Su;
import com.yvdedu.hospital.entity.Supply_unit;
import com.yvdedu.hospital.entity.User;
import com.yvdedu.hospital.service.DepotDailyService;

@Controller
@RequestMapping("business/depot/daily")
public class DepotDailyController {

	@Resource
	DepotDailyService depotDailyService;
	List<User> user;
	List<Department> department;
	List<Drug> Drug;
	int drug;
	List<Supply_unit> supply_unit;

	// 发药管理
	@RequestMapping("fy_admin")
	public String fy_admin(Department d_name, User Role_id, Drug drug, Model model) {
		user = depotDailyService.departmentQuery(4);
		System.out.println(user);
		department = depotDailyService.AQuery(d_name);
		// System.out.println(department);
		model.addAttribute("depotDailyService", user);
		model.addAttribute("d_name", department);
		return "business/depot/daily/fy_admin";

	}

	// 入库页面
	@RequestMapping("zy_medicine")
	public String zy_medicine(Model model) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		model.addAttribute("Date", format.format(new java.util.Date()));
		supply_unit = depotDailyService.Supply_unit_search();
		Drug = depotDailyService.rkxz();
		// System.out.println("12121212"+supply_unit);
		// System.out.println(1111);
		// Drug=depotDailyService.yp_rk_cx(name);
		// drug.setName(name);
		// System.out.println(name);
		// System.out.println(Drug);
		// model.addAttribute("drug", Drug);
		model.addAttribute("name_dw", supply_unit);
		model.addAttribute("Drug", Drug);
		return "business/depot/daily/zy_medicine";
	}

	// 入库查询
	@RequestMapping("add")
	@ResponseBody
	public Object add(Drug drug, String name) {
		Drug = depotDailyService.yp_rk_cx(name);
		int leng = Drug.size();
		drug.setName(name);
		// System.out.println(Drug);
		// System.out.println(leng);
		InputInfo inputInfo = new InputInfo();
		inputInfo.setDrug(Drug);
		inputInfo.setLeng(leng);
		return inputInfo;
	}

	// 入库增加
	@RequestMapping("addzj")
	@ResponseBody
	public Object rkzj(Drug drug, Model model, String name) {

		Drug = depotDailyService.yp_rk_cx(name);

		int max = 0;
		for (Drug drug2 : Drug) {
			int pc = drug2.getBatch();
			if (max < pc) {
				max = pc;
			} else if (max > pc || max == pc) {
				max = max;
			}
			// System.out.println("最大值----"+max);
		}

		if (Drug != null) {
			int batch = max + 1;
			drug.setBatch(batch);
			depotDailyService.add_Storage(drug);
			// System.out.println("---批次+1---"+batch);

		}
		// System.out.println("-----新加的数据-------"+Drug);

		model.addAttribute("acc", "true");
		model.addAttribute("url", "addzj");
		return "info/tips";
	}

	// 删除
	@RequestMapping("delete")
	public Object delete(int id, Model model) {
		depotDailyService.sc(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "zy_medicine");
		return "info/tips";
	}

	// 入库单位
	@RequestMapping("shenhe")
	public Object delete(Model model) {
		return "business/depot/daily/shenhe";
	}

	// 入库查询
	@RequestMapping("rk_search")
	public Object search(Model model, Supply_unit supply, String name) {
		supply_unit = depotDailyService.Supply_unit_cx(name);
		model.addAttribute("supply_unit", supply_unit);
		// System.out.println("入库单位查询:"+supply_unit);
		return "business/depot/daily/shenhe";
	}

	// 入库单位删除
	@RequestMapping("dw_delete")
	public Object dw_delete(Model model, int id) {
		depotDailyService.Supply_unit_sc(id);
		// System.out.println(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "shenhe");
		return "info/tips";
	}

	// 入库单位增加
	@RequestMapping("dw_add")
	public Object dw_add(Model model, Supply_unit supply_unit) {
		// 得到long类型当前时间
		long ltime = System.currentTimeMillis();
		// new日期对象
		Date date = new Date(ltime);
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String jl_date = dateFormat.format(date);
		supply_unit.setJl_date(jl_date);
		// 执行数据库增加操作
		depotDailyService.add_Supply_unit(supply_unit);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "shenhe");
		return "info/tips";
	}

	// 入库单位多条删除
	@RequestMapping("dt_dw_delete")
	public void dt_dw_delete(HttpServletRequest request, HttpServletResponse response) {

		String items = request.getParameter("ids");

		List<String> idlist = new ArrayList<String>();
		String[] strs = items.split(",");

		for (String str : strs) {
			idlist.add(str);
			System.out.println("id数组----" + str);
		}

		depotDailyService.batchDeleteUser(idlist);

	}

	// 发票号查询
	@RequestMapping("fp_search")
	public Object fp_search(Drug drug, Model model, int fpnumber, String sh) {
		Drug = depotDailyService.fp_cx(fpnumber, sh);
		model.addAttribute("fp_list", Drug);
		System.out.println("发票号:----" + Drug);
		return "business/depot/daily/bill";

	}

	// 入库发票删除
	@RequestMapping("fp_delete")
	public Object fp_delete(Model model, int id) {
		depotDailyService.drug_sc(id);
		// System.out.println(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "bill");
		return "info/tips";
	}

	// 入库发票多条删除
	@RequestMapping("dt_fp_delete")
	public void dt_fp_delete(HttpServletRequest request, HttpServletResponse response) {

		String items = request.getParameter("ids");

		List<String> idlist = new ArrayList<String>();
		String[] strs = items.split(",");

		for (String str : strs) {
			idlist.add(str);
			System.out.println("id数组----" + str);
		}

		depotDailyService.dt_batchDeleteUser(idlist);

	}

	// 多条审核
	@RequestMapping("zt_update")
	public void zt_update(Drug dr,HttpServletRequest request, HttpServletResponse response) {
		String items = request.getParameter("ids");
		String sh_name = request.getParameter("sh_name");		
		List<String> idlist = new ArrayList<String>();
		String[] strs = new String[99]; //必须new 字节长度
		strs = items.split(",");
		for (String str : strs) {
			idlist.add(str);
			
		}
		// 得到long类型当前时间
		long ltime = System.currentTimeMillis();
		// new日期对象
		Date date = new Date(ltime);
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String sh_date = dateFormat.format(date);
		depotDailyService.zt_update(idlist, sh_name,sh_date);
	}

	// 单据业务
	@RequestMapping("bill")
	public void bill() {
	}

	// 药品管理
	@RequestMapping("drugadmin")
	public String drugadmin(Integer pageNumber, Model model) {
		System.out.println(pageNumber);
		if (pageNumber == null) {
			pageNumber = 1;
		}
		Drug = depotDailyService.drugQuery((int) pageNumber);
		PageInfo page = new PageInfo(Drug);
		System.out.println(Drug);
		model.addAttribute("pageNumber", page.getPageNum());
		model.addAttribute("Pages", page.getPages());
		model.addAttribute("drugadmin", Drug);
		return "business/depot/daily/drugadmin";

	}

	// 药品id查询
	@RequestMapping("search")
	public String drugadmin(Drug drug, int ID_date, Model model) {
		System.out.println(ID_date);
		Drug = depotDailyService.IdQuery(ID_date);
		System.out.println(Drug);
		drug.setId(ID_date);
		model.addAttribute("drugadmin", Drug);
		return "business/depot/daily/drugadmin";

	}

	// 药品修改查询
	@RequestMapping("update")
	public String updatesearch(Drug drug, int id, Model model) {
		System.out.println(id);
		Drug = depotDailyService.xg_search(id);
		System.out.println(Drug);
		drug.setId(id);
		model.addAttribute("yao", Drug);
		return "business/depot/daily/drugadmin";

	}

	// 药品信息修改
	@RequestMapping("update_update")
	public String update_update(Drug drugl, Model model) {
		drug = depotDailyService.yao_update(drugl);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "drugadmin");
		return "info/tips";
	}

	// 药品有效期查询
	@RequestMapping("searchData")
	public String drugadmin(Drug drug, String yx_date, Model model) {
		System.out.println(yx_date);
		Drug = depotDailyService.DateQuery(yx_date);
		System.out.println(Drug);
		drug.setYx_date(yx_date);
		model.addAttribute("drugadmin", Drug);
		return "business/depot/daily/drugadmin";

	}

	// 药品有效期限检测
	@RequestMapping("detection")
	public void detection(Model model, Integer pageNumber) {
		if (pageNumber == null) {
			pageNumber = 1;
		}

		List<Drug> list = depotDailyService.query((int) pageNumber);
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageNumber", page.getPageNum());
		model.addAttribute("Pages", page.getPages());
		model.addAttribute("drug", list);
	}

	// 药品库存数量检测
	@RequestMapping("detection1")
	public void sum(Model model) {
		// List<Drug> list = depotDailyService.query();
		// model.addAttribute("drug", list);
	}

	// 药品单据核审检测
	@RequestMapping("detection2")
	public void hs(Model model) {
		// List<Drug> list = depotDailyService.query();
		// model.addAttribute("drug", list);
	}

	// 药品盘点Model model,Integer pageNumber,Drug drug
	@RequestMapping("inventory")
	public void inventory(Model model, Integer pageNumber) {
		if (pageNumber == null) {
			pageNumber = 1;
		}

		List<Drug> list = depotDailyService.query((int) pageNumber);
		// System.out.println(list);
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageNumber", page.getPageNum());
		model.addAttribute("Pages", page.getPages());
		model.addAttribute("drug", list);
	}

	// 实盘数录入
	@RequestMapping("inventory1")
	public void inventory1(Model model, Integer pageNumber) {
		if (pageNumber == null) {
			pageNumber = 1;
		}

		List<Su> list = depotDailyService.suquery(pageNumber);
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageNumber", page.getPageNum());
		model.addAttribute("Pages", page.getPages());
		model.addAttribute("suadd", list);
	}

	// 添加盘点单
	@RequestMapping("inventory1add")
	public Object inventory1add(Model model, Su su) {

		// 得到long类型当前时间
		long ltime = System.currentTimeMillis();
		// new日期对象
		Date date = new Date(ltime);
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String time = dateFormat.format(date);
		su.setSu_date(time);
		su.setState("正在盘点");

		depotDailyService.addsum(su);

		model.addAttribute("acc", "true");
		model.addAttribute("url", "inventory1");
		return "info/tips";

	}

	// 结束盘点单
	@RequestMapping("inventory1over")
	public Object inventory1over(Model model, int id) {

		depotDailyService.delesum(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "inventory1");
		return "info/tips";

	}

	// 删除盘点单
	@RequestMapping("inventory1dele")
	public Object inventory1dele(Model model, int id) {

		depotDailyService.delesum(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "inventory1");
		return "info/tips";

	}

	@RequestMapping("inventory2")
	public void inventory2(Model model, Integer pageNumber) {
		if (pageNumber == null) {
			pageNumber = 1;
		}

		// List<Drug> list = depotDailyService.query((int)pageNumber);
		// System.out.println(list);
		// PageInfo page = new PageInfo(list);
		// model.addAttribute("pageNumber",page.getPageNum());
		// model.addAttribute("Pages",page.getPages());
		// model.addAttribute("drug", list);
	}

	@RequestMapping("inventory3")
	public void inventory3(Model model, Integer pageNumber) {
		if (pageNumber == null) {
			pageNumber = 1;
		}

		// List<Drug> list = depotDailyService.query((int)pageNumber);
		// System.out.println(list);
		// PageInfo page = new PageInfo(list);
		// model.addAttribute("pageNumber",page.getPageNum());
		// model.addAttribute("Pages",page.getPages());
		// model.addAttribute("drug", list);
	}

	@RequestMapping("inventory4")
	public void inventory4(Model model, Integer pageNumber) {
		if (pageNumber == null) {
			pageNumber = 1;
		}

		List<Drug> list = depotDailyService.query((int) pageNumber);
		// System.out.println(list);
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageNumber", page.getPageNum());
		model.addAttribute("Pages", page.getPages());
		model.addAttribute("drug", list);
	}

}
